/**
 * 小储云商城
 * 模板视图js调用类 public
 * @type {{input_data: (function(*, *, *, *): string), data_class: (function(*): string), num: (function(): string), data_goods: (function(*): string)}}
 */
var template = {
    data_class: function (data) { //商品分类模板
        if (data.image == '') {
            data.image = 'assets/cloud/image/error_img.png';
        }
        var content = '<div class="layui-col-xs6 layui-col-sm4 layui-col-md3">\n' +
            '        <a href="?cid=' + data.cid + '"><div class="layui-card" style="border-radius: 0.5em">\n' +
            '           <div class="layui-card-body" style="padding: 0">\n' +
            '             <img style="height: 22vh" lay-src="' + data.image + '"  onerror="this.src=\'assets/cloud/image/error_img.png\'" width="100%" />\n' +
            '              </div>\n' +
            '                   <div class="layui-card-hader" style="text-align: center">\n' +
            '              <div class=" layui-elip">\n' +
            '                 ' + data.name + '\n' +
            '              </div>\n' +
            '       <span class="layui-word-aux" style="font-size: 0.8em">\n' +
            '            ' + data.number + '个商品\n' +
            '        </span>\n' +
            '     </div>\n' +
            '  </div></a>\n' +
            '</div>';
        return content;
    }, SharePoster: function (gid) {
        layer.msg('正在生成商品分享海报...', {icon: 16, time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=SharePoster",
            data: {gid: gid},
            dataType: "json",
            success: function (data) {
                if (data.code == 1) {
                    layer.alert('<img src="' + data.src + '" width=300 heigth=450 />', {
                        area: ['340px', '490px'],
                        title: false,
                        btn: false, shade: [0.8, '#000'],
                        shadeClose: true,
                    })
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            },
            error: function () {
                layer.alert('生成失败！');
            }
        });
    },
    kefu: function (qq) {
        layer.alert('<center><img src="http://q4.qlogo.cn/headimg_dl?dst_uin=' + qq + '&spec=100" style="border-radius: 30em;width: 5em;height: 5em;margin-bottom: 1em;" /><br>平台客服QQ：' + qq + '<hr><a href="http://wpa.qq.com/msgrd?v=3&uin=' + qq + '&site=qq&menu=yes" target="_blank" class="layui-btn layui-btn-fluid layui-btn-radius layui-btn-sm layui-btn-normal btssn">点击联系Ta</a></center>', {
            area: ['300px', '200px'],
            title: false,
            btn: false, shade: [0.8, '#000'],
            shadeClose: true,
        })
    },
    data_goods: function (data) { //商品列表模板
        if (data.quota != 0) {
            var quota = '<br>' + data.quantity + '个';
        } else {
            var quota = '<br><font color=#EF5350>库存不足</font>'
        }
        if (data.image == '') {
            data.image = 'assets/cloud/image/error_img.png';
        }
        var content = '<div class="layui-col-xs6 layui-col-sm4 layui-col-md3">\n' +
            '        <a href="?cid=' + data.cid + '&gid=' + data.gid + '"><div class="layui-card" style="border-radius: 0.5em">\n' +
            '           <div class="layui-card-body" style="padding: 0">\n' +
            '               <img  style="height: 22vh" lay-src="' + data.image + '" onerror="this.src=\'assets/cloud/image/error_img.png\'" width="100%" >\n' +
            '            </div>\n' +
            '         <div class="layui-card-hader" style="text-align: center">\n' +
            '                                    <div class=" layui-elip">\n' +
            '                                        ' + data.name + '\n' +
            '                                    </div>\n' +
            '                 <span class="layui-word-aux" style="font-size: 0.8em">\n' +
            '                     商品价格 | ' + (data.price - 0) + '元 ' + quota + '\n' +
            '                </span>\n' +
            '           </div>\n' +
            '      </div></a>\n' +
            '  </div>';
        return content;
    },
    input_data: function (name, placeholder, id, addstr) {//商品输入框模板
        if (addstr != '') {
            var content = '<div class="layui-form-item">\n' +
                '                            <label class="layui-form-label"  id="var_c' + id + '" >' + name + '</label>\n' +
                '                            <div class="layui-input-block"  style="padding-right: 80px !important;" >\n' +
                '                                <input type="text" onblur="checkInput(\'var_c' + id + '\',\'input_val_' + id + '\')" placeholder="' + placeholder + '"  id="input_val_' + id + '"    class="layui-input" />\n' +
                '                            </div>' + addstr + '\n' +
                '                        </div>';
        } else {
            var content = '<div class="layui-form-item">\n' +
                '                            <label class="layui-form-label" id="var_c' + id + '" >' + name + '</label>\n' +
                '                            <div class="layui-input-block">\n' +
                '                                <input type="text"  onblur="checkInput(\'var_c' + id + '\',\'input_val_' + id + '\')" placeholder="' + placeholder + '"  id="input_val_' + id + '"  class="layui-input">\n' +
                '                            </div>\n' +
                '                        </div>';
        }
        return content;
    },
    select_data: function (selectname, addstr, i) { //多选框构建
        var content = '<div class="layui-form-item">\n' +
            '                            <label class="layui-form-label"  id="var_c' + i + '">' + selectname + '</label>\n' +
            '                            <div class="layui-input-block">\n' +
            '                                <select lay-verify="required"  id="input_val_' + i + '">\n' +
            '                                    <option value=""></option>' + addstr + '\n' +
            '                                </select>\n' +
            '                            </div>\n' +
            '                        </div>';
        return content;
    },
    num: function () { //商品下单数量模板
        var content = '<div class="layui-form-item">\n' +
            '                            <label class="layui-form-label">下单份数</label>\n' +
            '                            <div class="layui-input-block">\n' +
            '                                <input type="number" min="1" value="1"  placeholder="请填写商品下单份数！"  id="num"  class="layui-input">\n' +
            '                            </div>\n' +
            '                        </div>';
        return content;
    }
    , success: function (data) {
        if (data.data['CarriageTem'] != undefined && data.data['freight'] != "") { //启用运费模板
            if (data.data['freight'] != "-1") {
                CarriageTem.start(data.data['CarriageTem'], data.data['freight'], data.data['price']);
            }
        }
        $('#points').on('click', function () {
            var that = this;
            layer.tips('<font color=#fff>此商品支持积分兑换哦,每日限量快来领取吧！</font>', that, {tips: [1, '#000']}); //在元素的事件回调体中，follow直接赋予this即可
        });
        if ($_GET['cid'] == undefined || $_GET['cid'] == '') {
            var url = '/'
        } else {
            var url = '/?cid=' + $_GET['cid'];
        }
        layui.use('util', function () {
            var util = layui.util;
            util.fixbar({
                bar1: '&#xe65c;'
                , bar2: '&#xe657;'
                , bgcolor: '#FF5252'
                , click: function (type) {
                    if (type === 'bar1') {
                        location.href = url;
                    } else if (type === 'bar2') {
                        layer.closeAll();
                        if (window.innerWidth > 460) {
                            area = ['460px', '90%'];
                        } else {
                            area = ['90%', '90%'];
                        }
                        layer.open({
                            title: '我的购物车',
                            type: 2,
                            shade: false,
                            area: area,
                            maxmin: true,
                            skin: 'layui-layer-rim',
                            content: '/?mod=cart',
                            zIndex: 999999, //重点1
                            success: function (layero) {
                                layer.iframeAuto();
                            }
                        });
                    }
                }
            });
        });

        $("#cart_sum").html(' (' + data.data.cart_count + ')');

        $("#level").html('<font color="#ff4500">' + data.data.level + '</font>');
        var level_arr = $("#gootsp").attr('data-level_arr');
        if (level_arr == '-1') {
            layer.tips('恭喜！您当前为最高等级密价！', '#level', {tips: [1, '#651FFF'], zIndex: 1});
            $("#level").attr('onclick', 'layer.msg(\'恭喜！您当前为最高等级密价！\')');
        } else {
            layer.tips('<font color="white">此商品有更多优惠哦！<br>点击查看获取！</font>', '#level', {tips: [3, '#6200EA'], zIndex: 1});
            $("#level").attr('onclick', 'layer.alert(\'' + unescape(level_arr) + '\',{title:\'密价等级表\',btn:[\'确定\',\'升级密价\'],shade: [0.8, \'#000\'],shadeClose:true,btn2:function(layero,index){window.open(\'/user/grade.php\')}})');
        }
    },
    query_tem: function (data) { //订单模板
        $("#order_all,#order_a,#order_b,#order_c,#order_d").html('');
        var content_error = '<div class="layui-col-xs12 layui-col-sm6"><div class="layui-card qt-card">\n' +
            '              <div class="layui-card-header">\n' +
            '               <p style="width: 70%" class="layui-elip">没有查询到相关订单</p>\n' +
            '                       </div>\n' +
            '                        <div class="layui-card-body">\n' +
            '                       <div class="layui-row layui-col-space10">\n' +
            '                            <div class="layui-col-xs4" style="text-align: center">\n' +
            '                                <img src="' + logo + '">\n' +
            '                            </div><div class="layui-col-xs8" style="text-align: center">\n' +
            '                                登陆后台可永久保存订单哦<hr><a href="user">进入后台</a>\n' +
            '                            </div>\n' +
            '         </div>\n' +
            '    </div>\n' +
            '</div></div><hr>';

        if (data.code == 1) {
            var content = ''; //全部订单
            var content_a = ''; //待提交
            var a = 0;
            $.each(data.data, function (key, val) {

                switch (val['state']) {
                    case '1': //成功
                        var state = '<font color="#43A047">已完成</font>';
                        break;
                    case '2': //待处理
                        var state = '<font color="#651FFF">待处理</font>';
                        break;
                    case '3': //异常
                        var state = '<font color="#FF1744">异常中</font>';
                        break;
                    case '4': //正在处理
                        var state = '<font color="#F57C00">正在处理</font>';
                        break;
                    case '5': //已退款
                        var state = '<font color="#424242">已退款</font>';
                        break;
                    case '6': //售后中
                        var state = '<font color="#8b0000">售后中</font>';
                        break;
                    case '7': //已评价结单
                        var state = '<font color="#8fbc8f">已评价</font>';
                        break;
                    default:
                        var state = '<font color="#607D8B">未知状态</font>';
                        break;
                }

                if (val.payment == '积分兑换') {
                    val.price = (val.price - 0).toFixed(0) + '积分';
                } else if (val.payment == '免费领取') {
                    val.price = '<font color="#2e8b57">免费商品</font>';
                } else {
                    val.price = (val.price - 0).toFixed(2) + '元';
                }

                btne = '';

                if (val.token !== "0") {
                    msg_token = (val.token).join('<br>');
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="layer.alert(\'卡密信息为：<br>' + msg_token + '\',{title:\'您购买的卡密信息\'})">\n' +
                        '                             卡密\n' +
                        '                      </button>';
                }

                if (val.logistics != "-1") {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="layer.alert(\'快递单号信息：<br>' + val.logistics + '\<hr>查询快递方法：复制快递单号，手机打开菜鸟包裹查询即可！\',{title:\'您的快递单号\'})">\n' +
                        '                             物流\n' +
                        '                      </button>';
                }

                if (val['state'] != 5 && val['state'] != 7) {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.tickets(' + val.id + ')">\n' +
                        '                             售后\n' +
                        '                      </button>';
                }

                if (val['take'] == 2 && val.state != 7) {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_mark(' + val.id + ')"  >\n' +
                        '                             评价\n' +
                        '                      </button>';
                }

                if (val['take'] == 1 && val['state'] != 5 && val['state'] != 7) {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_take(' + val.id + ')" >\n' +
                        '                             确认收货\n' +
                        '                      </button>';
                }

                btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_delete(' + val.id + ')">\n' +
                    '                             删除\n' +
                    '                      </button>';

                content += '<div class="layui-card qt-card">\n' +
                    '              <div class="layui-card-header">\n' +
                    '               <p style="width: 70%" class="layui-elip">' + val.name + '</p>\n' +
                    '                    <span class="layui-layout-right layui-elip"\n' +
                    '                           style="width:30%;text-align: right;margin-right: 0.5em">\n' +
                    '                          ' + state + '\n' +
                    '                    </span>\n' +
                    '                       </div>\n' +
                    '                        <div class="layui-card-body">\n' +
                    '                       <div class="layui-row layui-col-space10">\n' +
                    '                            <div class="layui-col-xs4">\n' +
                    '                                <a href="/?gid=' + val.gid + '&cid=' + val.cid + '"><img ' + (a <= 6 ? '' : 'lay-') + 'src="' + val.image + '"/></a>\n' +
                    '                            </div>\n' +
                    '                       <div class="layui-col-xs8"\n' +
                    '                            style="font-size: 0.8em;color:black;font-family: \'微软雅黑\'">\n' +
                    '                            下单时间：' + val.addtitm + '<br>\n' +
                    '                            商品总价：' + val.price + '<br>\n' +
                    '                            购买方式：' + val.payment + '\n' +
                    '                      </div>\n' +
                    '                  <div style="width: 100%;text-align: right">\n' +
                    '                       <button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_data(' + val.id + ')">\n' +
                    '                             详情\n' +
                    '                      </button>' + btne + '\n' +
                    '               </div>\n' +
                    '         </div>\n' +
                    '    </div>\n' +
                    '</div>';
                ++a;
            });

            var b = 0;
            $.each(data.data2, function (key, val) {
                btns = '';
                if (val.payment == '积分兑换') {
                    val.price = (val.price - 0).toFixed(0) + '积分';
                } else if (val.payment == '免费领取') {
                    val.price = '<font color="#2e8b57">免费商品</font>';
                } else {
                    val.price = (val.price - 0).toFixed(2) + '元';
                }

                switch (val['type']) {
                    case '2':
                        var state = '<font color="#20b2aa">待提交服务器</font>';
                        btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.SubmitOrder(' + val.id + ')">\n' +
                            '                             手动提交\n' +
                            '                      </button>';
                        break;
                    case '3':
                        var state = '<font color="#1e90ff">待付款</font>';
                        btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.OrderPay(' + val.id + ')">\n' +
                            '                             点击付款\n' +
                            '                      </button>';
                        btns += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.OrderDel(' + val.id + ')">\n' +
                            '                             点击删除\n' +
                            '                      </button>';
                        break;
                    default:
                        var state = '<font color="#ff4500">已失效</font>';
                        btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.OrderDel(' + val.id + ')">\n' +
                            '                             点击删除\n' +
                            '                      </button>';
                        break;
                }

                content_a += '<div class="layui-card qt-card" id="order_' + val.id + '">\n' +
                    '              <div class="layui-card-header">\n' +
                    '               <p style="width: 70%" class="layui-elip">' + val.name + '</p>\n' +
                    '                    <span class="layui-layout-right layui-elip"\n' +
                    '                           style="width:30%;text-align: right;margin-right: 0.5em">\n' +
                    '                          ' + state + '\n' +
                    '                    </span>\n' +
                    '                       </div>\n' +
                    '                        <div class="layui-card-body">\n' +
                    '                       <div class="layui-row layui-col-space10">\n' +
                    '                            <div class="layui-col-xs4">\n' +
                    '                                <a href="/?gid=' + val.gid + '&cid=' + val.cid + '"><img ' + (b <= 6 ? '' : 'lay-') + 'src="' + val.image + '"/></a>\n' +
                    '                            </div>\n' +
                    '                       <div class="layui-col-xs8"\n' +
                    '                            style="font-size: 0.8em;color:black;font-family: \'微软雅黑\'">\n' +
                    '                            商品总价：' + val.price + '<br>\n' +
                    '                            购买方式：' + val.payment + '<br>\n' +
                    '                            创建时间：' + val.addtime + '<br>\n' +
                    '                            提交时间：' + (val.endtime == undefined ? '尚未付款,无法提交' : val.endtime) + '<br>\n' +
                    '                            订单备注：' + val.remark + '<br>\n' +
                    '                      </div>\n' +
                    '                  <div style="width: 100%;text-align: right">\n' + btns +
                    '               </div>\n' +
                    '         </div>\n' +
                    '    </div>\n' +
                    '</div>';
                ++b;
            });


            if (content == '') {
                content = content_error;
            }

            if (content_a == '') {
                content_a = content_error;
            }

            $("#order_all").html(content);
            $("#order_a").html(content_a);
            content = '';
        } else {
            $("#order_all,#order_a,#order_b").html(content_error);
        }

        $.ajax({
            type: "post",
            url: "/api.php?act=OrdersTesting",
            dataType: "json",
        });
        $.ajax({
            type: "post",
            url: "/api.php?act=SubmitOrder",
            data: {num: 2},
            dataType: "json",
        });
    },
    OrderDel: function (id) {
        layer.load(2, {time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=OrderDel",
            data: {id: id},
            dataType: "json",
            success: function (data) {
                if (data.code == 1) {
                    $("#order_" + id).hide(100);
                    layer.msg(data.msg, {icon: 1});
                    OrderManagement.OrderQuery('OrderAll');
                } else layer.msg(data.msg, {icon: 2});
            },
            error: function () {
                layer.closeAll();
                layer.alert('删除失败！');
            }
        });
    },
    SubmitOrder: function (id) {
        layer.load(2, {time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=SubmitOrder",
            data: {id: id},
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1});
                    OrderManagement.OrderQuery('OrderAll');
                } else layer.msg(data.msg, {icon: 2});
            },
            error: function () {
                layer.closeAll();
                layer.alert('提交失败！');
            }
        });
    },
    OrderPay: function (id) {
        layer.load(2, {time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=OrderPay",
            data: {id: id},
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                if (data.code == 1) {
                    var inst = layer.msg(data.msg, {
                        btn: ['确定'], time: 9999999, btn1: function (layero, index) {
                            layer.close(inst);
                            OrderManagement.OrderQuery('OrderAll');
                        }
                    })
                } else if (data.code == 2) {
                    location.href = data.url;
                    setTimeout(function () {
                        layer.alert('<font size="4" color="#2e8b57">若未跳转付款界面,请点击下方付款按钮！</font>', {
                            title: '温馨提示!',
                            btn: ['付款'], time: 9999999, btn1: function (layero, index) {
                                location.href = data.url;
                            }
                        })
                    }, 500);
                } else {
                    layer.alert(data.msg, {icon: 2, title: '温馨提示'});
                }
            },
            error: function () {
                layer.closeAll();
                layer.alert('购买失败！');
            }
        });
    },
};

if ($_GET['cid'] == undefined && $_GET['name'] == undefined) {
    if (checkstandM == undefined) {
        merchandise.cid_data();//调用分类
        layui.use('util', function () {
            var util = layui.util;
            util.fixbar({
                bar1: '&#xe657;'
                , bgcolor: '#FF5252'
                , click: function (type) {
                    if (type === 'bar1') {
                        layer.closeAll();
                        if (window.innerWidth > 460) {
                            area = ['460px', '90%'];
                        } else {
                            area = ['90%', '90%'];
                        }
                        layer.open({
                            title: '我的购物车',
                            type: 2,
                            shade: false,
                            area: area,
                            maxmin: true,
                            skin: 'layui-layer-rim',
                            content: '/?mod=cart',
                            zIndex: 999999, //重点1
                            success: function (layero) {
                                layer.iframeAuto();
                            }
                        });
                    }
                }
            });
        });
    }
} else if ($_GET['cid'] != undefined && $_GET['gid'] == undefined) {
    merchandise.gid_data($_GET['cid'], $_GET['name']);
    layui.use('util', function () {
        var util = layui.util;
        util.fixbar({
            bar1: '&#xe65c;'
            , bar2: '&#xe657;'
            , bgcolor: '#FF5252'
            , click: function (type) {
                if (type === 'bar1') {
                    location.href = './';
                } else if (type === 'bar2') {
                    layer.closeAll();
                    if (window.innerWidth > 460) {
                        area = ['460px', '90%'];
                    } else {
                        area = ['90%', '90%'];
                    }
                    layer.open({
                        title: '我的购物车',
                        type: 2,
                        shade: false,
                        area: area,
                        maxmin: true,
                        skin: 'layui-layer-rim',
                        content: '/?mod=cart',
                        zIndex: 999999, //重点1
                        success: function (layero) {
                            layer.iframeAuto();
                        }
                    });
                }
            }
        });
    });
} else if ($_GET['cid'] != undefined && $_GET['gid'] != undefined) {
    /**
     * 调用商品下单界面模块
     */
    $("#shop").show();
    merchandise.shop_data($_GET['gid']);


}
